package com.fulihui.wgj.admin.config;

import com.fulihui.common.util.JedisUtil;
import com.fulihui.wgj.admin.config.properties.AdminProperties;
import com.fulihui.wgj.admin.config.properties.InterceptorProperties;
import com.fulihui.wgj.admin.config.properties.JwtProperties;
import com.fulihui.wgj.admin.security.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Objects;

/**
 * @author: Levon
 * @version: v 0.1 2018-06-14 16:22
 */
@Configuration
@EnableConfigurationProperties({InterceptorProperties.class})
public class InterceptorConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private InterceptorProperties interceptorProperties;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Autowired
    private AdminProperties adminProperties;

    @Autowired
    private JedisUtil jedisUtil;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(jwtInterceptor)
                .addPathPatterns(new String[]{"/**"});

        if (Objects.nonNull(interceptorProperties.getExcludePaths())
                && interceptorProperties.getExcludePaths().length > 0) {
            interceptorRegistration.excludePathPatterns(interceptorProperties.getExcludePaths());
        }
        jwtInterceptor.setJwtProperties(jwtProperties);
        jwtInterceptor.setAdminProperties(adminProperties);
        jwtInterceptor.setJedisUtil(jedisUtil);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(new String[]{"/swagger-resources"})
                .addResourceLocations(new String[]{"classpath:/META-INF/resources/"});
    }
}
